package com.example.myapplication2;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.text.Editable;
import android.text.TextWatcher;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.utils.LogUtils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTextView;
    private TextView mTextView1;
    private TextView mTextView4;
    private LinearLayout mLayout;
    private Button mButton;
    private TextView mText;

    private EditText editText;

    private ImageView mImageView;

    private static final String TAG = "MainActivity";

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        mTextView = findViewById(R.id.myAppliacation1);
        /*mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });*/

        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Log.d("test1","==========mTextView onClick");
//                Log.d(TAG, "===========mTextView onClick=============");
                LogUtils.d(TAG, "----------mTextView onClick-----------");
                onViewClick(1);
            }
        });

        mTextView1 = findViewById(R.id.myAppliacation2);
        mTextView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Log.d(TAG, "===========mTextView1 onClick=============");
                LogUtils.d(TAG, "-----------mTextView1 onClick-----------");
                showToast("text view 2 click");
            }
        });

        mLayout = findViewById(R.id.main);
        mLayout.setOnClickListener(this);

        mTextView4 = findViewById(R.id.myAppliacation4);
//        mTextView4.setText("第四个猫猫喵喵叫");
        mTextView4.setText(getResources().getString(R.string.app_name));
        mTextView4.setVisibility(View.VISIBLE);
//        mTextView4.setBackgroundResource(R.mipmap.bg_gray);
        mTextView4.setBackgroundColor(ContextCompat.getColor(this, R.color.green));
        mTextView4.setTextSize(getResources().getDimension(R.dimen.text_size));
        mTextView4.setTextColor(ContextCompat.getColor(this, R.color.white));

        LogUtils.d(TAG, "mTextView4现实的文字是" + mTextView4.getText().toString());
        mTextView4.setOnClickListener(this);

        mButton = findViewById(R.id.button);

        mButton.setText("click me");
//        mButton.setBackground(getDrawable(R.drawable.bg_button_selector));
//        mButton.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.bg_button_selector));
        mButton.setBackgroundColor(ContextCompat.getColor(this,R.color.color_selector));
        mButton.setTextSize(getResources().getDimension(R.dimen.text_size));
//        mButton.setTextColor(getColor(R.color.black));


        mText = findViewById(R.id.mytextView);
        mText.setOnClickListener(this);
        mButton.setOnClickListener(this);
        mButton.setEnabled(true);


        mTextView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast(mTextView4.getText().toString());
            }
        });

        editText = findViewById(R.id.edit);
        editText.requestFocus();

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                LogUtils.d(TAG,"-----------beforeTextChanged-------------"+s.toString());
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                LogUtils.d(TAG,"-----------onTextChanged-------------"+s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
                LogUtils.d(TAG,"-----------afterTextChanged-------------"+s.toString());
            }
        });

        mImageView = findViewById(R.id.image);
        mImageView.setImageDrawable(ContextCompat.getDrawable(MainActivity.this,R.drawable.radio_selector));
        mImageView.setBackground(ContextCompat.getDrawable(MainActivity.this,R.drawable.ic_launcher_background));
        mImageView.setBackgroundResource(R.drawable.bg_button_selector);
        mImageView.setOnClickListener(this);
    }

    private void onViewClick(int id) {
//        Log.d("test1","======onViewClick=======");
//        Log.e(TAG, "=============onViewClick=============== " + id);
        LogUtils.e(TAG, "--------------onViewClick-----------");
        if (id == 1) {
            showActivity();
        }
    }

    private void showActivity() {
//        Log.d("test1","==========showActivity");
//        Log.d(TAG, "===========showActivity=============");
        LogUtils.i(TAG, "-----------showActivity-----------");
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        startActivity(intent);
    }

    private void showToast(String text) {
//        Log.d(TAG, "===========showToast=============" + text);
        LogUtils.i(TAG, "------------showToast-------------" + text);
        Toast.makeText(this, text, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            LogUtils.d(TAG, "Button click");
        } else if (id == R.id.myAppliacation4) {
            LogUtils.d(TAG, "TextView4 click");
        } else if (id == R.id.main) {
            LogUtils.d(TAG, "Main click");
        }else if (id == R.id.image){
            LogUtils.d(TAG, "Image click");
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }
}